package com.example.managemyself.data.database;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.example.managemyself.data.model.AIConversation;

import java.util.List;

/**
 * AI对话记录DAO
 */
@Dao
public interface AIConversationDao {
    
    @Insert
    long insert(AIConversation conversation);
    
    @Update
    void update(AIConversation conversation);
    
    @Delete
    void delete(AIConversation conversation);
    
    @Query("SELECT * FROM ai_conversations WHERE userId = :userId ORDER BY updatedAt DESC")
    LiveData<List<AIConversation>> getConversationsByUser(int userId);
    
    @Query("SELECT * FROM ai_conversations WHERE userId = :userId AND isStarred = 1 ORDER BY updatedAt DESC")
    LiveData<List<AIConversation>> getStarredConversations(int userId);
    
    @Query("SELECT * FROM ai_conversations WHERE userId = :userId AND category = :category ORDER BY updatedAt DESC")
    LiveData<List<AIConversation>> getConversationsByCategory(int userId, String category);
    
    @Query("SELECT * FROM ai_conversations WHERE id = :conversationId")
    LiveData<AIConversation> getConversationById(int conversationId);
    
    @Query("SELECT * FROM ai_conversations WHERE userId = :userId AND (title LIKE :searchQuery OR summary LIKE :searchQuery) ORDER BY updatedAt DESC")
    LiveData<List<AIConversation>> searchConversations(int userId, String searchQuery);
    
    @Query("DELETE FROM ai_conversations WHERE userId = :userId")
    void deleteAllByUser(int userId);
    
    @Query("SELECT COUNT(*) FROM ai_conversations WHERE userId = :userId")
    LiveData<Integer> getConversationCount(int userId);
    
    @Query("SELECT DISTINCT category FROM ai_conversations WHERE userId = :userId")
    LiveData<List<String>> getCategories(int userId);
} 